package HttpClientUtil;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import us.codecraft.webmagic.proxy.Proxy;

import java.io.IOException;

public class HttpClientUtil {
    private static PoolingHttpClientConnectionManager poolManger;
    private static RequestConfig config;


    public static CloseableHttpClient getHttpClient(){
        if(poolManger == null){
            //1配置连接池的参数
            poolManger = new PoolingHttpClientConnectionManager();
            poolManger.setMaxTotal(100);//最大连接数
            poolManger.setDefaultMaxPerRoute(20);//路由最大连接数
        }
        if(config == null){
            //2配置连接池中连接的参数
            config = RequestConfig.custom()
                    .setConnectTimeout(50000)//发送请求的超时时间
                    .setSocketTimeout(20000)//响应超时时间
                    .setConnectionRequestTimeout(5000)//从连接池中获取的超时时间
                    .build();
        }

        //拿到httpClient
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(poolManger)
                .setDefaultRequestConfig(config)
                .build();

        return  httpClient;

    }

    /**
     * 执行get请求返回结果
     * @param url
     * @return
     */
    public static String doGet(String url){
        String result = "";
        CloseableHttpClient httpClient = getHttpClient();
        //2 创建httpGet请求
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"); // 设置请求头消息User-Agent
        //3 执行请求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if(200 == statusCode){
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity,"UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  result;
    }

    /**
     * 使用代理执行get请求返回结果
     * @param url
     * @return
     */
    public static String doGetProxy(String url, Proxy proxy){
        String result = "";

        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(5000)//发送请求的超时时间
                .setSocketTimeout(5000)//响应超时时间
                .setConnectionRequestTimeout(500)//从连接池中获取的超时时间
                .setProxy(new HttpHost(proxy.getHost(),proxy.getPort()))
                .build();
        //拿到httpClient
        CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultRequestConfig(config)
                .build();

        //2 创建httpGet请求
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"); // 设置请求头消息User-Agent
        //3 执行请求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if(200 == statusCode){
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity,"UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  result;
    }

    /**
     * 执行post请求返回结果
     * @param url
     * @return
     */
    public static String doPost(String url){
        String result = "";
        CloseableHttpClient httpClient = getHttpClient();
        //2 创建httpPost请求
        HttpPost httpPost = new HttpPost(url);
        //3 执行请求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if(200 == statusCode){
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity,"UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  result;
    }


}
